package com.cloud.zhimao.common.api;

import com.cloud.zhimao.common.config.exception.CloudException;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

/**
 * QQ互联API
 * @author Long
 */
@Component
public class QQInterConnection {


    public AccessToken getAccessToken(HttpServletRequest req){
        try {
            return (new Oauth()).getAccessTokenByRequest(req);
        }catch (QQConnectException e) {
            throw new CloudException("授权信息获取失败", e, "用户信息获取失败");
        }

    }

    public String getOpenID(String token){
        try {
            return new OpenID(token).getUserOpenID();
        } catch (QQConnectException e) {
            throw new CloudException("授权信息获取失败", e, "OpenID获取失败");
        }

    }



    public UserInfoBean getUserInfo(String token, String openID){
        try {
            return new UserInfo(token, openID).getUserInfo();
        } catch (QQConnectException e) {
            throw new CloudException("授权信息获取失败", e, "用户信息获取失败");
        }
    }

}
